﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class sysadmin_SystemInfo_CountProject : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["OrderList"] = "";  //
            ViewState["OrderType"] = " desc ";//排序方式
            string search = GetSearch();
            //Response.Write(search);
            //Response.End();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_CountProject_Name.Text = GetLanageValueByKey("lbl_CountProject_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CountProject_InfoTitle.Text = GetLanageValueByKey("lbl_CountProject_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append("select TTrainProject.TrainproID,TTrainProject.TrainproName as proname,TTrainProject.TrainproCreatetime as createtime,(select count(TrainclaID) from TTrainClass where FKTrainclaTrainproID=TTrainProject.TrainproID) as classnum,(select count(ProresID) from TProjectResource where FKProresTrainproID=TTrainProject.TrainproID) as coursenum,(select sum(TrainclaStudentNum) from TTrainClass where FKTrainclaTrainproID=TTrainProject.TrainproID) as studentnum from TTrainProject where ");

        if (!string.IsNullOrEmpty(txt_Name.Text))
        {
            sbSearch.Append(string.Format(" TTrainProject.TrainproName like '%{0}%' and ", txt_Name.Text));
        }

        sbSearch.Append(" 1=1 ");
        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = obj.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
        PreAspNetPager(AspNetPager1);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = new DataTable();

        string orderlist = ViewState["OrderList"].ToString();
        if (!string.IsNullOrEmpty(orderlist))
            search += " order by " + orderlist + ViewState["OrderType"].ToString();
        else
            search += " order by TTrainProject.TrainproID desc";
        //Response.Write(search);
        //Response.End();

        //string orderList = " OnlineuserLoginTime desc ";
        dt = obj.Query(search, LMSPageSize, pageIndex);

        
        rpt_CountProject.DataSource = dt;
        rpt_CountProject.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_CurrentSearch_Click(object sender, EventArgs e)
    {
        
    }

    protected void rpt_CountProject_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "orderby")
        {
            ViewState["OrderList"] = e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search, AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
}
